﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Entities.MetaDataSchema
{
    public class Table : Entities.Entity
    {

        #region Properties

        //private Database _database;
        public Database ParentDatabase
        {
            set { _Parent = value; }
            get { return _Parent as Database; }
        }
        private string _schema;
        public string Schema
        {
            set { _schema = value; }
            get { return _schema; }
        }

        private int _primaryColumnCount = 0;
        public int PrimaryColumnCount
        {
            set { _primaryColumnCount = value; }
            get { return _primaryColumnCount; }
        }

        private string _tableId;
        public string TableID
        {
            set { _tableId = value; }
            get { return _tableId; }
        }

        private List<Column> _columns = new List<Column>();
        public List<Column> Columns
        {
            set { _columns = value; }
            get { return _columns; }
        }

        public static Table GetByName(List<Table> tables, string searchName)
        {
            foreach (Table row in tables)
            {
                if (row.Name == searchName)
                    return row;
            }
            return null;
        }

        public override void UnSelect()
        {
            
            foreach (Column cols in Columns)
            {
                cols.UnSelect();
            }

        }
        #endregion

        #region ICloneable Members

        public override object Clone()
        {
            if (!this.EntitySelected)
                return null;
            Table tbl = new Table();
            tbl.Name = this.Name;
            tbl.EntitySelected = this.EntitySelected;
            tbl.Id = this.Id;
            tbl.PrimaryColumnCount = this.PrimaryColumnCount;
            tbl.Schema = this.Schema;
            tbl.TableID = this.TableID;
            foreach (Column col in this.Columns)
            {
                Column newCol = col.Clone() as Column;
                if (newCol != null)
                {
                    newCol.Parent = tbl;
                    tbl.Columns.Add(newCol);
                }
            }
            return tbl;

        }

        #endregion
    }
}
